package com.itheima.ai.controller;

import com.itheima.ai.entity.vo.MessageVO;
import com.itheima.ai.repository.ChatRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.MessageType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@CrossOrigin("*")
@RequiredArgsConstructor
@RequestMapping("/ai")
@RestController
public class HistoryController {

    private final ChatMemory chatMemory;

    private final ChatRepository chatRepository;

    @GetMapping("/history/{type}")
    public Set<String> histories(@PathVariable("type") String type) {
        return chatRepository.getChatIds(type);
    }

    @GetMapping("/history/{type}/{chatId}")
    public List<MessageVO> historyById(@PathVariable("chatId") String chatId) {
        return chatMemory.get(chatId, Integer.MAX_VALUE)
                .stream()
                .filter(message -> message.getMessageType() == MessageType.USER || message.getMessageType() == MessageType.ASSISTANT)
                .map(MessageVO::new)
                .toList();
    }
}
